import LogService from "../service/LogService";

export class BuffUtils {

  /**
   * 数组转换为 buff
   * @param arr
   * @returns {ArrayBufferLike}
   */
  static arrToBuffer(arr){
    const typedArray = new Uint8Array(arr);
    return typedArray.buffer;
  }

  static buffToArr(buff){
    let data = new Uint8Array(buff);
    let arr = [];
    data.forEach(value => arr.push(value));
    return arr;
  }

  static to16(arr){
    let temp = arr.map(value => {
      value = Number(value);
      return value.toString(16);
    })

    return temp;
  }

}

export class Queue {
  list = [];
  size = 0;

  constructor(size) {
    this.size = size;
  }

  clear = () =>{
    this.list = [];
  }

  addArr = (arr) => {

    arr.forEach(v =>{
      this.add(v);
    })
    return true;
  }

  add = (data) => {
    if (data == null) {
      return false;
    }
    //如果传递了size参数就设置了队列的大小
    if (this.list.length == this.size) {
      this.pop();
      LogService.error("push err list is full");
    }
    this.list.unshift(data);
    return true;
  }

  getData = ()=>{
    return this.list.pop();
  }

  getArr = (len)=>{
    let arr = [];
    for (let i = 0; i < len; i++) {
      arr.push(this.list.pop())
    }
    return arr;
  }

  getSize = ()=>{
    return this.list.length;
  }
}
